package com.sxz.mall.ware.service.impl;

import com.sxz.mall.common.constant.WareConstant;
import com.sxz.mall.ware.entity.PurchaseDetailEntity;
import com.sxz.mall.ware.service.PurchaseDetailService;
import com.sxz.mall.ware.service.WareSkuService;
import com.sxz.mall.ware.vo.MergeVO;
import com.sxz.mall.ware.vo.PurchaseDoneVO;
import com.sxz.mall.ware.vo.PurchaseItemDoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxz.mall.common.utils.PageUtils;
import com.sxz.mall.common.utils.Query;

import com.sxz.mall.ware.dao.PurchaseDao;
import com.sxz.mall.ware.entity.PurchaseEntity;
import com.sxz.mall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService detailService;

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0).or().eq("status", 1);
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Integer merge(MergeVO mergeVO) {
        Long purchaseId = mergeVO.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);

            purchaseId = purchaseEntity.getId();
        }
        // 状态为新建和已分配的才可以合单
        PurchaseEntity purchase = this.getById(purchaseId);
        if (purchase.getStatus() != WareConstant.PurchaseStatusEnum.CREATED.getCode() &&
                purchase.getStatus() != WareConstant.PurchaseStatusEnum.ASSIGED.getCode()) {
            return -1;
        }

        final Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> list = mergeVO.getItems().stream().map(i -> {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            entity.setId(i);
            entity.setPurchaseId(finalPurchaseId);
            entity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGED.getCode());
            return entity;
        }).filter(id -> {
            PurchaseDetailEntity item = detailService.getById(id);
            return item.getStatus() == WareConstant.PurchaseDetailStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseDetailStatusEnum.ASSIGED.getCode();
        }).collect(Collectors.toList());
        if (list != null && list.size() > 0) {
            detailService.updateBatchById(list);
        }

        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);


        return 1;
    }

    @Transactional
    @Override
    public void receive(List<Long> ids) {
        // 领取的采购单只能是新建和已分配
        List<PurchaseEntity> purchaseEntityList = ids.stream().map(id -> {
            return this.getById(id);
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGED.getCode()) {
                return true;
            }
            return false;
        }).map(item -> {
            item.setUpdateTime(new Date());
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            return item;
        }).collect(Collectors.toList());

        // 更新采购单状态为已领取
        this.updateBatchById(purchaseEntityList);
        // 更新采购项为正在采购  一个采购单有多个采购项
        for (Long id : ids) {
            List<PurchaseDetailEntity> list = detailService.listDetailByPurchaseId(id);
            List<PurchaseDetailEntity> list1 = list.stream().map(item -> {
                PurchaseDetailEntity entity = new PurchaseDetailEntity();
                entity.setId(item.getId());
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return entity;
            }).collect(Collectors.toList());

            detailService.updateBatchById(list1);
        }
    }

    @Transactional
    @Override
    public void done(PurchaseDoneVO vo) {
        Long id = vo.getId();
        Boolean flag = true;
        List<PurchaseItemDoneVO> items = vo.getItems();
        List<PurchaseDetailEntity> list = new ArrayList<>();
        for (PurchaseItemDoneVO item : items) {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                flag = false;
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
            } else {
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());

                // 如果是采购成功的，需要入库
                PurchaseDetailEntity detailEntity = detailService.getById(item.getItemId());
                wareSkuService.addStock(detailEntity);
            }
            entity.setId(item.getItemId());
            list.add(entity);
        }
        detailService.updateBatchById(list);
        // 1\改变采购单的状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() :
                WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);

        // 2、改变采购项的状态

        // 3、采购项数量入库
    }

}